﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace UIT_CV_Management
{
    public class DiaDiemThucTap
    {
        public String maDiaDiem;
        private String tenTruong;
        private String tenVien;
        private String tenNuoc;

        // Database
        Database myDatabase = Database.shareDatabase;
        public DiaDiemThucTap(String _tenTruong, String _tenVien, String _tenNuoc)
        {
            maDiaDiem = Database.getID("DiaDiemThucTap", "MaDiaDiem");

            //properties
            tenTruong = _tenTruong;
            tenVien = _tenVien;
            tenNuoc = _tenNuoc;
        }
        public DiaDiemThucTap(String _maDiaDiem)
        {
            QueryResult result = myDatabase.Query("SELECT * FROM DiaDiemThucTap WHERE MaDiaDiem = '" + _maDiaDiem + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;
                DataRow row = table.Rows[0];

                maDiaDiem = row["MaDiaDiem"].ToString();
                tenTruong = row["TenTruong"].ToString();
                tenVien = row["TenVien"].ToString();
                tenNuoc = row["TenNuoc"].ToString();

            }
        }

        public Boolean Save()
        {
            String sqlDiaDiemThucTap = "INSERT INTO DiaDiemThucTap VALUES('"
                + maDiaDiem + "',N'"
                + tenTruong + "',N'"
                + tenVien + "',N'" 
                + tenNuoc + "')";
            QueryResult result = myDatabase.Query(sqlDiaDiemThucTap);

            if (result.ReturnCode == 409)
                return false;
            return true;
        }
        public Boolean Update()
        {
            bool isDupplicated = Database.isDupplicate("DiaDiemThucTap", "MaDiaDiem", maDiaDiem);

            // update
            if (isDupplicated)
            {
                String sqlUpdate = "UPDATE DiaDiemThucTap SET "
                    + "TenTruong = N'" + tenTruong
                    + "', TenVien = N'" + tenVien
                    + "', TenNuoc = N'" + tenNuoc
                    + "' WHERE MaDiaDiem = '" + maDiaDiem + "'";
                QueryResult result = myDatabase.Query(sqlUpdate);

                if (result.ReturnCode == 409)
                    return false;
                else
                {
                    return true;
                }

            }
            return false;
        }
        public Boolean Delete()
        {
            String sqlDelete = "DELETE FROM DiaDiemThucTap WHERE MaDiaDiem = '" + maDiaDiem + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                return false;
            else
            {
                return true;
            }
        }
    }
}
